/**
 * @author ahammer
 * @date 2009
 */

/*
 * This file is part of Iqm.
 * Copyright (c) 2010-2011 Helmut Ahammer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package main;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.awt.event.MouseEvent;
import java.net.URL;

public class Board extends JPanel implements ActionListener, MouseListener {
	
	private static final long serialVersionUID = 1L;
	public static JTextArea     textArea = null;
    private final static String newline  = "\n"; 
    private static JPopupMenu 	popup;
	private static JFrame 		boardJFrame;
       
    public Board() {
        super(new GridBagLayout());

        //textArea = new JTextArea(8, 60); //createshowgui
        textArea = new JTextArea();
        textArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(textArea);

        //Add Components to this panel.
        GridBagConstraints c = new GridBagConstraints();
        c.gridwidth = GridBagConstraints.REMAINDER;

        c.fill = GridBagConstraints.HORIZONTAL;
        
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1.0;
        c.weighty = 1.0;
        add(scrollPane, c);
                
        //popup
        popup = new JPopupMenu();
        JMenuItem menuItem = new JMenuItem("Clear");
        menuItem.setActionCommand("clear");
        menuItem.addActionListener(this);
        popup.add(menuItem);
        //menuItem = new JMenuItem("Another popup menu item");
        //menuItem.addActionListener(this);
        //popup.add(menuItem);

                      
      //Add listener to components that can bring up popup menus.
        MouseListener popupListener = new PopupListener();
        textArea.addMouseListener(popupListener);
                                        
    }
      
    public static void appendTexln (String arg) {
    	textArea.append(arg + newline);
    	System.out.println(arg);
    	
        //Make sure the new text is visible, even if there
        //was a selection in the text area.
        textArea.setCaretPosition(textArea.getDocument().getLength());
        boardJFrame.toFront();
    }
    public static void appendTex (String arg) {
    	textArea.append(arg);
    	System.out.print(arg);
    	
        //Make sure the new text is visible, even if there
        //was a selection in the text area.
        textArea.setCaretPosition(textArea.getDocument().getLength());
        boardJFrame.toFront();
    }

    public void mouseClicked(MouseEvent e) {
    	// TODO Auto-generated method stub
   		/*if ((e.getClickCount() == 1) && (e.getButton() == 3)) {  //right button
			System.out.println("Rechtsklick");
						
		}*/
	}
    
    
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event dispatch thread.
     */
    public static void createAndShowGUI() {
    	
    	
    	//Create and set up the window.
        boardJFrame = new JFrame();
        //BoardJ boardJFrame = newBoard();
        //boardJFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        boardJFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        Dimension dimScreen = Toolkit.getDefaultToolkit().getScreenSize();
	    //int width = dimScreen.width-4;
	    int width  = 448;
	    int height = 170;
        int xLoc = 2;
        int yLoc = dimScreen.height - 390;
        boardJFrame.setSize(width, height);
        boardJFrame.setLocation(xLoc, yLoc);
		//frame.setLocationRelativeTo( null 
        boardJFrame.setTitle("Board");
		URL url = ClassLoader.getSystemResource("imgs/IqmLogoGrey32_v5.png");		
		if(url!=null)boardJFrame.setIconImage(new ImageIcon(url).getImage());
		//frame.setContentPane(getJContentPane());
        boardJFrame.setResizable(true);
        //Add contents to the window.
        boardJFrame.add(new Board());
        
        //Display the window.
        //boardJFrame.pack();
        boardJFrame.setVisible(true);

    }
	/**
	 * This method destroys the GUI
	*/
	public static void destroyGUI() {
		boardJFrame.setVisible(false);
		boardJFrame.dispose();
		boardJFrame = null;	
	}	
        
    public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
    	if ("clear".equals(e.getActionCommand())) {
						
			String s = textArea.getText();
	        int end = s.length();
	        textArea.replaceRange("", 0, end);
	        System.out.println("Board cleared");
		}
	}
    
    
    class PopupListener extends MouseAdapter {
        public void mousePressed(MouseEvent e) {
            maybeShowPopup(e);
        }

        public void mouseReleased(MouseEvent e) {
            maybeShowPopup(e);
        }

        private void maybeShowPopup(MouseEvent e) {
            if (e.isPopupTrigger()) {
                popup.show(e.getComponent(),
                           e.getX(), e.getY());
            }
        }
    }


				
	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
    

